package parser

import (
	"cloudDevice2/sampleapi/protobuf"
	"fmt"
	"log"

	"cloudDevice2/sampleapi/utils"

	"github.com/golang/protobuf/proto"
)

func ProceedOldMan(pbData []byte) {
	omInfo := &protobuf.OM0Report{}
	err := proto.Unmarshal(pbData, omInfo)
	if err != nil {
		log.Println(fmt.Sprintf("parse oldman error: %s", err.Error()))
		return
	}

	seconds := int64(omInfo.GetDateTime().GetDateTime().GetSeconds())
	//data time
	rt_time_str := utils.ParsePbDateTime(seconds)

	battery := omInfo.GetBattery().GetLevel()
	rssi_uint32 := omInfo.GetRssi()
	var rssi int32
	if rssi_uint32 > uint32(INT32_MAX) {
		rssi = int32(^(rssi_uint32)+1) * -1
	} else {
		rssi = int32(rssi_uint32)
	}
	//battery and rssi
	log.Println(fmt.Sprintf("----%s battery:%d, rssi:%d", rt_time_str, battery, rssi))

	if omInfo.GetHealth() != nil {
		rtHealth := omInfo.GetHealth()
		var distance float32 = float32(rtHealth.GetDistance()) * 0.1
		var calorie float32 = float32(rtHealth.GetCalorie()) * 0.1
		step := rtHealth.GetSteps()
		//step/distance/calorie
		log.Println(fmt.Sprintf("----%s step:%d, distance:%f, calorie:%f", rt_time_str,
			step, distance, calorie))
	}

	//gnss location
	//*notice: the location upload by device is in WGS_84 coordinate system, not GCJ_02
	trackList := omInfo.GetTrackData()
	if trackList != nil && len(trackList) > 0 {
		for _, track := range trackList {
			seconds := int64(track.GetTime().GetDateTime().GetSeconds())
			gnss_time_str := utils.ParsePbDateTime(seconds)
			locate_type := track.GetGpsType()
			log.Println(fmt.Sprintf("----gnss time:%s,lon:%f,lat:%f,loc type:%d", gnss_time_str,
				track.GetGnss().GetLongitude(),
				track.GetGnss().GetLatitude(), locate_type.String()))
		}
	}
}
